package com.ax.api.config.base;

import com.ax.api.config.AjaxRes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class BaseController<T> {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    public String AJAX_FAIL = "error:falure!";
    public String AJAX_SUCCESS = "success";

    /**
     * 返回数据
     */
    AjaxRes ajaxRes = getAjaxRes();


    /**
     * 得到ModelAndView
     */
    public ModelAndView getModelAndView() {
        return new ModelAndView();
    }

    public AjaxRes getAjaxRes() {
        return new AjaxRes();
    }

    /**
     * 得到request对象
     */
    public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * 得到图片上传项目路径
     *
     * @return
     */
    public static String getRealPath() {
        return getRequest().getServletContext().getRealPath("/uploadImage");
    }

    /**
     * 得到response
     *
     * @return
     */
    public static HttpServletResponse getResponse() {
        return ((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();
    }



    public static void logBefore(Logger logger, String interfaceName) {
        logger.info("");
        logger.info("start");
        logger.info(interfaceName);
    }

    public static void logAfter(Logger logger) {
        logger.info("end");
        logger.info("");
    }



    /**
     * 判断操作是否成功
     */
    public AjaxRes isAjaxOperation(int col, String param) {
        if (col == 0) {
            ajaxRes.setFailMsg(AJAX_FAIL);
        } else {
            ajaxRes.setSucceed(param, AJAX_SUCCESS);
        }
        logger.info(ajaxRes.toJSONString());
        return ajaxRes;
    }

    public AjaxRes isAjaxOperation(List<Map<String, String>> list) {
        if (org.springframework.util.StringUtils.isEmpty(list)) {
            ajaxRes.setFailMsg(AJAX_FAIL);
        } else {
            ajaxRes.setSucceed(list, AJAX_SUCCESS);
        }
        logger.info(ajaxRes.toJSONString());
        return ajaxRes;
    }

    public AjaxRes isAjaxOperation(Page<Object> page) {
        if (org.springframework.util.StringUtils.isEmpty(page)) {
            ajaxRes.setFailMsg(AJAX_FAIL);
        } else {
            ajaxRes.setSucceed(page, AJAX_SUCCESS);
        }
        logger.info(ajaxRes.toJSONString());
        return ajaxRes;
    }

    public AjaxRes isAjaxOperation(String str) {
        if (org.springframework.util.StringUtils.isEmpty(str)) {
            ajaxRes.setFailMsg(AJAX_FAIL);
        } else {
            ajaxRes.setSucceed(str, AJAX_SUCCESS);
        }
        logger.info(ajaxRes.toJSONString());
        return ajaxRes;
    }

    public static String romdom() {
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < 4; i++) {
            int s = random.nextInt(8) + 1;
            buffer.append(s);
        }

        return buffer.toString();

    }

    public static void getParamMap(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (Entry<String, String[]> entry : parameterMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue()[0];
            System.err.println(key + "\t" + value);
        }
    }


    public static Map<String, Object> stringToMap(String obj, String fistSymbol, String secondSymbol) {
        Map<String, Object> map = new HashMap<>();
        //String code="code:0,msg:sueecss";
        String a[] = obj.split(fistSymbol);
        if (fistSymbol != null) {
            for (int i = 0; i < a.length; i++) {
                if (secondSymbol != null) {
                    for (int k = 0; k < a[i].split(secondSymbol).length; k++) {
                        String b[] = a[i].split(secondSymbol);
                        if (k % 2 == 0) {
                            map.put(b[k], b[k + 1]);
                        }
                    }
                }
            }
        }

        for (Entry<String, Object> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
        return map;
    }

    // 判断一个字符串是否都为数字
    public static boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }


}
